package com.gpl.controller;

import java.util.ArrayList;
import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.gpl.domain.Point;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.gpl.domain.ImitatePosition;
import com.gpl.service.IImitatePositionService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 电子围栏表Controller
 * 
 * @author luna
 * @date 2023-05-24
 */
@RestController
@RequestMapping("/position")
public class ImitatePositionController extends BaseController
{
    @Autowired
    private IImitatePositionService imitatePositionService;

    /**
     * 查询电子围栏表列表
     */
    @RequiresPermissions("gpl:position:list")
    @GetMapping("/list")
    public TableDataInfo list(ImitatePosition imitatePosition)
    {
        startPage();
        List<ImitatePosition> list = imitatePositionService.selectImitatePositionList(imitatePosition);
        return getDataTable(list);
    }

    /**
     * 导出电子围栏表列表
     */
    @RequiresPermissions("gpl:position:export")
    @Log(title = "电子围栏表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ImitatePosition imitatePosition)
    {
        List<ImitatePosition> list = imitatePositionService.selectImitatePositionList(imitatePosition);
        ExcelUtil<ImitatePosition> util = new ExcelUtil<ImitatePosition>(ImitatePosition.class);
        util.exportExcel(response, list, "电子围栏表数据");
    }

    /**
     * 获取电子围栏表详细信息
     */
    @RequiresPermissions("gpl:position:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(imitatePositionService.selectImitatePositionById(id));
    }

    /**
     * 新增电子围栏表
     */
    @RequiresPermissions("gpl:position:add")
    @Log(title = "电子围栏表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ImitatePosition imitatePosition)
    {
        return toAjax(imitatePositionService.insertImitatePosition(imitatePosition));
    }

    /**
     * 修改电子围栏表
     */
    @RequiresPermissions("gpl:position:edit")
    @Log(title = "电子围栏表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ImitatePosition imitatePosition)
    {
        return toAjax(imitatePositionService.updateImitatePosition(imitatePosition));
    }

    /**
     * 删除电子围栏表
     */
    @RequiresPermissions("gpl:position:remove")
    @Log(title = "电子围栏表", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(imitatePositionService.deleteImitatePositionByIds(ids));
    }


    @GetMapping("/dianzi")
    public boolean isInPolygon(Point point) {
        ImitatePosition imitatePosition = imitatePositionService.list(1L);
        ObjectMapper objectMapper = new ObjectMapper();
        List<String[]> list = JSON.parseObject(imitatePosition.getPosition(), List.class);
        ArrayList<Point> pts = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            String[] doubles = JSON.parseObject(String.valueOf(list.get(i)), String[].class);
            // System.out.println(doubles[0]);
            // System.out.println(doubles[1]);
            Point point1 = new Point();
            point1.setLng(Double.valueOf(doubles[0]));
            point1.setLat(Double.valueOf(doubles[1]));
            pts.add(point1);
        }
        // 宋","lng":"111.749180","phone":"18636836705","createTime":"2023
        point.setLat(40.03367);
        point.setLng(116.38943);
        int N = pts.size();
        boolean boundOrVertex = true;
        int intersectCount = 0;// 交叉点数量
        double precision = 2e-10; // 浮点类型计算时候与0比较时候的容差
        Point p1, p2;// 临近顶点
        Point p = point; // 当前点

        p1 = pts.get(0);
        for (int i = 1; i <= N; ++i) {
            if (p.equals(p1)) {
                return boundOrVertex;
            }

            p2 = pts.get(i % N);
            if (p.getLng() < Math.min(p1.getLng(), p2.getLng()) || p.getLng() > Math.max(p1.getLng(), p2.getLng())) {
                p1 = p2;
                continue;
            } else if (p.getLng() > Math.min(p1.getLng(), p2.getLng()) && p.getLng() < Math.max(p1.getLng(), p2.getLng())) {  // 射线穿过算法
                if (p.getLat() <= Math.max(p1.getLat(), p2.getLat())) {
                    if (p1.getLng() == p2.getLng() && p.getLat() >= Math.min(p1.getLat(), p2.getLat())) {
                        return boundOrVertex;
                    }
                    if (p1.getLat() == p2.getLat()) {
                        if (p1.getLat() == p.getLat()) {
                            return boundOrVertex;
                        } else {
                            ++intersectCount;
                        }
                    } else {
                        double xinters = (p.getLng() - p1.getLng()) * (p2.getLat() - p1.getLat()) / (p2.getLng() - p1.getLng()) + p1.getLat();
                        if (Math.abs(p.getLat() - xinters) < precision) {
                            return boundOrVertex;
                        }

                        if (p.getLat() < xinters) {
                            ++intersectCount;
                        }
                    }
                }
            } else {
                if (p.getLng() == p2.getLng() && p.getLat() <= p2.getLat()) {
                    Point p3 = pts.get((i + 1) % N);
                    if (p.getLng() >= Math.min(p1.getLng(), p3.getLng()) && p.getLng() <= Math.max(p1.getLng(), p3.getLng())) {
                        ++intersectCount;
                    } else {
                        intersectCount += 2;
                    }
                }
            }
            p1 = p2;
        }
        return intersectCount % 2 != 0;
    }
}
